package com.simeon.mobilemacysnet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.simeon.mobilemacysnet.R;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button buttonLogin; 
	private Button buttonEmail; 
	private Button buttonJenkins; 
	private Button buttonJira; 
	private Button buttonNewJira; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButtonLogin();
        addListenerOnButtonEmail();
        addListenerOnButtonJenkins();
        addListenerOnButtonJira();
        addListenerOnButtonNewJira();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.copy:
            	copyFiles();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
	public void addListenerOnButtonLogin() {
		 
		buttonLogin = (Button) findViewById(R.id.buttonLogin);
 
		buttonLogin.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				loginNetwork();				
			}
 
		});
 
	}
	
	public void addListenerOnButtonEmail() {
		 
		buttonEmail = (Button) findViewById(R.id.buttonEmail);
 
		buttonEmail.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				loginEmail();				
			}
 
		});
 
	}
	
	public void addListenerOnButtonJenkins() {
		 
		buttonJenkins = (Button) findViewById(R.id.buttonJenkins);
 
		buttonJenkins.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View arg0) {
				jenkins();				
			}
 
		});
 
	}
	
	public void addListenerOnButtonJira() {
		 
		buttonJira = (Button) findViewById(R.id.buttonJira);
		
		buttonJira.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View arg0) {
				jira();				
			}
 
		});
 
	}
	
	public void addListenerOnButtonNewJira() {
		 
		buttonNewJira = (Button) findViewById(R.id.buttonNewJira);
		
		buttonNewJira.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View arg0) {
				newJira();				
			}
 
		});
 
	}
	
	private void loginNetwork() {
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///sdcard/loginNetwork.html"));
		browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
		startActivity(browserIntent);		
	}
	
	private void loginEmail() {
		Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("file:///sdcard/loginEmail.html"));
		browserIntent2.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
		startActivity(browserIntent2);
	}
	
	private void jenkins() {
		Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://remote.macys.net/jenkins/,DanaInfo=11.120.101.7,Port=8080+"));
		browserIntent3.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
		startActivity(browserIntent3);		
	}
	
	private void jira() {
		Intent browserIntent4 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://remote.macys.net/,DanaInfo=jira+"));
		browserIntent4.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
		startActivity(browserIntent4);		
	}
	
	private void newJira() {
		Intent browserIntent5 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://remote.macys.net/,DanaInfo=11.120.101.89,Port=8080+"));
		browserIntent5.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
		startActivity(browserIntent5);		
	}
	
	private void copyFiles() {
		copyAssets();
	}
	
	private void copyAssets() {
	    AssetManager assetManager = getAssets();
	    String[] files = null;
	        try {
				files = assetManager.list("");
			} catch (IOException e1) {
				System.out.println("Error jh: " + e1.getMessage());
			}
	    for(String filename : files) {
	        InputStream in = null;
	        OutputStream out = null;
	          try {
				in = assetManager.open(filename);
	          out = new FileOutputStream("/mnt/sdcard/" + filename);
	          copyFile(in, out);
	          in.close();
	          in = null;
	          out.flush();
	          out.close();
	          out = null;
				} catch (IOException e) {
					System.out.println("Error jh: " + e.getMessage());
				}
	    }
	}
	private void copyFile(InputStream in, OutputStream out) throws IOException {
	    byte[] buffer = new byte[1024];
	    int read;
	    while((read = in.read(buffer)) != -1){
	      out.write(buffer, 0, read);
	    }
	}
}
